<?php

declare(strict_types=1);

namespace App\CustHub\Helpers;

use App\CustHub\Mapper\ChRobotMapper;
use App\CustHub\Model\ChRobot;
use App\Robot\Service\RobotApiService;
use Hyperf\Di\Annotation\Inject;
use Mine\Exception\NormalStatusException;

class Robot
{
    protected mixed $robotId;

    protected bool $isThrow;

    protected ChRobot $robotInfo;


    #[Inject]
    protected ChRobotMapper $robotMapper;

    public function __construct(mixed $robotId,bool $isThrow = false)
    {
        $this->robotId = $robotId;
        $this->isThrow = $isThrow;
    }

    /**
     * 获取机器人服务
     * @return RobotApiService|false
     */
    public function getApiService(): RobotApiService|false
    {
        $robotInfo = $this->robotMapper->findById($this->robotId);
        if(empty($robotInfo) || $robotInfo['login_status'] != 1){
            if($this->isThrow){
                throw new NormalStatusException('请先登陆企微号');
            }else{
                return false;
            }
        }
        $this->robotInfo = $robotInfo;
        return make(RobotApiService::class, [$robotInfo['server_id'], $robotInfo['robot_key']]);
    }

    public function getRobotInfo(): ChRobot
    {
        return $this->robotInfo;
    }
}